package com.qiyi.player.wonder.plugins.scenetile.view
{
   import com.iqiyi.components.panelSystem.impls.BasePanel;
   import com.qiyi.player.wonder.common.status.Status;
   import com.qiyi.player.wonder.common.vo.UserInfoVO;
   import common.CommonBg;
   import scenetile.FocusCloseBtn;
   import flash.display.Sprite;
   import flash.display.Loader;
   import flash.text.TextField;
   import flash.display.Shape;
   import scenetile.PlayNumIcon;
   import common.CommonGreenBtn;
   import scenetile.AlreadyFocusBtn;
   import gs.TweenLite;
   import com.qiyi.player.wonder.plugins.scenetile.SceneTileDef;
   import com.qiyi.player.wonder.common.ui.FastCreator;
   import flash.events.TextEvent;
   import flash.events.MouseEvent;
   import flash.events.Event;
   import flash.events.IOErrorEvent;
   import flash.events.SecurityErrorEvent;
   import flash.net.URLRequest;
   import com.qiyi.player.wonder.body.BodyDef;
   import flash.display.DisplayObjectContainer;
   import com.iqiyi.components.global.GlobalStage;
   import flash.net.navigateToURL;
   import com.qiyi.player.wonder.common.config.SystemConfig;
   
   public class SceneTileFocusView extends BasePanel
   {
      
      public function SceneTileFocusView(param1:DisplayObjectContainer, param2:Status, param3:UserInfoVO) {
         super(NAME,param1);
         this._status = param2;
         this._userInfoVO = param3;
         this.init();
      }
      
      public static const NAME:String = "com.qiyi.player.wonder.plugins.scenetile.view.SceneTileFocusView";
      
      private var _status:Status;
      
      private var _userInfoVO:UserInfoVO;
      
      private var _bg:CommonBg;
      
      private var _closeBtn:FocusCloseBtn;
      
      private var _headImageContainer:Sprite;
      
      private var _headImageLoader:Loader;
      
      private var _tfVideoName:TextField;
      
      private var _shapeVideoNumBG:Shape;
      
      private var _tfVideoNum:TextField;
      
      private var _playNumIcon:PlayNumIcon;
      
      private var _tfPlayNum:TextField;
      
      private var _tfFocusNum:TextField;
      
      private var _focusBtn:CommonGreenBtn;
      
      private var _alreadyFocusBtn:AlreadyFocusBtn;
      
      private var _tfFocus:TextField;
      
      private var _focusInfo:Object = null;
      
      public function set focusInfo(param1:Object) : void {
         this._focusInfo = param1;
         if(this._focusInfo == null)
         {
            return;
         }
         if(!(this._focusInfo.uploaderImage == undefined) || !(this._focusInfo.uploaderName == undefined) || !(this._focusInfo.videoCount == undefined) || !(this._focusInfo.playCount == undefined) || !(this._focusInfo.focusCount == undefined))
         {
            this.setPanelInfo(this._focusInfo.uploaderImage,this._focusInfo.uploaderName,this._focusInfo.videoCount,this._focusInfo.playCount,this._focusInfo.focusCount);
         }
      }
      
      public function setFocusResult(param1:Boolean) : void {
         if(!param1)
         {
            return;
         }
         if(this._focusInfo == null)
         {
            return;
         }
         this._tfFocus.text = "已关注";
         this._alreadyFocusBtn.visible = true;
         this._focusBtn.visible = false;
         TweenLite.killTweensOf(this.onCloseBtnClick);
         TweenLite.delayedCall(SceneTileDef.FOCUS_SUCCESS_SHOW_TIME / 1000,this.onCloseBtnClick);
         if(this._focusInfo.focusCount != undefined)
         {
            this._tfFocusNum.text = "关注数：" + this.numberToString(this._focusInfo.focusCount + 1).replace(new RegExp("(?=(?!\\b)(?:\\d{3})+(?!\\d))","g"),",");
         }
      }
      
      private function init() : void {
         this._bg = new CommonBg();
         this._bg.width = 290;
         this._bg.height = 90;
         addChild(this._bg);
         this._headImageContainer = new Sprite();
         this._headImageContainer.x = 5;
         this._headImageContainer.y = 10;
         this._headImageContainer.useHandCursor = this._headImageContainer.buttonMode = true;
         addChild(this._headImageContainer);
         this._tfVideoName = FastCreator.createLabel("视频名称",16777215,16,"",true);
         this._tfVideoName.x = 82;
         this._tfVideoName.y = 10;
         this._tfVideoName.width = this._bg.width - 24;
         addChild(this._tfVideoName);
         this._shapeVideoNumBG = new Shape();
         this._shapeVideoNumBG.graphics.beginFill(0,0.5);
         this._shapeVideoNumBG.graphics.drawRect(5,65,70,20);
         this._shapeVideoNumBG.graphics.endFill();
         addChild(this._shapeVideoNumBG);
         this._tfVideoNum = FastCreator.createLabel("视频数量",16777215,10);
         this._tfVideoNum.x = 16;
         this._tfVideoNum.y = 62;
         addChild(this._tfVideoNum);
         this._playNumIcon = new PlayNumIcon();
         this._playNumIcon.x = 83;
         this._playNumIcon.y = 45;
         addChild(this._playNumIcon);
         this._tfPlayNum = FastCreator.createLabel("4.39亿",16777215,12,"left");
         this._tfPlayNum.x = 87 + this._playNumIcon.width;
         this._tfPlayNum.y = 39;
         addChild(this._tfPlayNum);
         this._tfFocusNum = FastCreator.createLabel("关注数：250,395",16777215,12,"left");
         this._tfFocusNum.x = this._tfPlayNum.x + 50;
         this._tfFocusNum.y = 39;
         addChild(this._tfFocusNum);
         this._focusBtn = new CommonGreenBtn();
         this._focusBtn.width = 60;
         this._focusBtn.height = 20;
         this._focusBtn.x = 216;
         this._focusBtn.y = 60;
         addChild(this._focusBtn);
         this._alreadyFocusBtn = new AlreadyFocusBtn();
         this._alreadyFocusBtn.x = this._focusBtn.x;
         this._alreadyFocusBtn.y = this._focusBtn.y;
         this._alreadyFocusBtn.visible = false;
         addChild(this._alreadyFocusBtn);
         this._tfFocus = FastCreator.createLabel("关注",16777215,12);
         this._tfFocus.x = this._focusBtn.x + 15;
         this._tfFocus.y = this._focusBtn.y;
         addChild(this._tfFocus);
         this._tfFocus.mouseEnabled = this._tfFocus.selectable = false;
         this._closeBtn = new FocusCloseBtn();
         this._closeBtn.x = this._bg.width - this._closeBtn.width / 2 - 2;
         this._closeBtn.y = -this._closeBtn.height / 2;
         addChild(this._closeBtn);
         this._tfVideoName.addEventListener(TextEvent.LINK,this.onImageOrNameClick);
         this._headImageContainer.addEventListener(MouseEvent.CLICK,this.onImageOrNameClick);
         this._closeBtn.addEventListener(MouseEvent.CLICK,this.onCloseBtnClick);
         this._focusBtn.addEventListener(MouseEvent.CLICK,this.onFocusBtnClick);
      }
      
      private function setPanelInfo(param1:String, param2:String, param3:int, param4:int, param5:int) : void {
         if((this._headImageLoader) && (this._headImageLoader.parent))
         {
            this._headImageContainer.removeChild(this._headImageLoader);
            this._headImageLoader = null;
         }
         if(param1)
         {
            this._headImageLoader = new Loader();
            this._headImageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,this.onComplete);
            this._headImageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
            this._headImageLoader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onIOError);
            this._headImageLoader.load(new URLRequest(param1));
            this._headImageLoader.mouseChildren = this._headImageLoader.mouseEnabled = false;
         }
         this._tfVideoName.htmlText = "<a href=\'event:event\'>" + param2 + "</a>";
         this._tfVideoNum.text = "共" + param3 + "个视频";
         this._tfPlayNum.text = this.numberToString(param4).replace(new RegExp("(?=(?!\\b)(?:\\d{3})+(?!\\d))","g"),",");
         this._tfFocusNum.text = "关注数：" + this.numberToString(param5).replace(new RegExp("(?=(?!\\b)(?:\\d{3})+(?!\\d))","g"),",");
      }
      
      private function onComplete(param1:Event) : void {
         this._headImageContainer.addChild(this._headImageLoader);
         this._headImageLoader.width = this._headImageLoader.height = 70;
         this._headImageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.onComplete);
         this._headImageLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
         this._headImageLoader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onIOError);
      }
      
      private function onIOError(param1:Event) : void {
         this._headImageLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE,this.onComplete);
         this._headImageLoader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR,this.onIOError);
         this._headImageLoader.contentLoaderInfo.removeEventListener(SecurityErrorEvent.SECURITY_ERROR,this.onIOError);
      }
      
      private function numberToString(param1:int) : String {
         if(param1 >= 100000000)
         {
            return Math.round(param1 / 1000000) / 100 + "亿";
         }
         if(param1 >= 1000000)
         {
            return Math.round(param1 / 1000) / 10 + "万";
         }
         return param1.toString();
      }
      
      public function onUserInfoChanged(param1:UserInfoVO) : void {
         this._userInfoVO = param1;
      }
      
      public function onAddStatus(param1:int) : void {
         this._status.addStatus(param1);
         switch(param1)
         {
            case SceneTileDef.STATUS_FOCUS_OPEN:
               TweenLite.killTweensOf(this.onCloseBtnClick);
               TweenLite.delayedCall(SceneTileDef.FOCUS_SHOW_TIME / 1000,this.onCloseBtnClick);
               this.open();
               break;
         }
      }
      
      public function onRemoveStatus(param1:int) : void {
         this._status.removeStatus(param1);
         switch(param1)
         {
            case SceneTileDef.STATUS_FOCUS_OPEN:
               TweenLite.killTweensOf(this.onCloseBtnClick);
               this.close();
               break;
         }
      }
      
      public function onResize(param1:int, param2:int) : void {
         y = param2 - this._bg.height - BodyDef.VIDEO_BOTTOM_RESERVE - 30;
      }
      
      override public function open(param1:DisplayObjectContainer = null) : void {
         if(!isOnStage)
         {
            super.open(param1);
            dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_FocusOpen));
         }
      }
      
      override public function close() : void {
         if(isOnStage)
         {
            super.close();
            this._alreadyFocusBtn.visible = false;
            this._focusBtn.visible = true;
            dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_FocusClose));
         }
      }
      
      override protected function onAddToStage() : void {
         super.onAddToStage();
         this._tfFocus.text = "关注";
         this.onResize(GlobalStage.stage.stageWidth,GlobalStage.stage.stageHeight);
      }
      
      override protected function onRemoveFromStage() : void {
         super.onRemoveFromStage();
      }
      
      private function onFocusBtnClick(param1:MouseEvent) : void {
         dispatchEvent(new SceneTileEvent(SceneTileEvent.Evt_FocusBtnClick));
         this.close();
      }
      
      private function onImageOrNameClick(param1:Event) : void {
         if((this._focusInfo) && !(this._focusInfo.uploaderID == undefined))
         {
            GlobalStage.setNormalScreen();
            navigateToURL(new URLRequest(SystemConfig.FOCUS_PERSON_PAGE + this._focusInfo.uploaderID),"_self");
         }
      }
      
      private function onCloseBtnClick(param1:MouseEvent = null) : void {
         this.close();
      }
   }
}
